﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Core.BOL;
using System.Web.Profile;

namespace Core.Providers
{
    public class CustomMembershipProvider : SqlMembershipProvider
    {
        private string[] characters =  new[]
        {
            "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n",
            "2", "3", "4", "5", "6", "7", "8", "9",
            "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
        };

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            MembershipUser mu = Membership.GetUser(username);

            if (mu != null)
            {
                //Delete Comments
                CommentRepository cr = new CommentRepository();
                cr.ArchiveUserComments((Guid)mu.ProviderUserKey);
                cr.Save();

                //Delete My album
                MyAlbumRepository mar = new MyAlbumRepository();
                mar.DeleteUserImages((Guid)mu.ProviderUserKey);
                mar.Save();

                //Delete Rate
                RatingRepository rr = new RatingRepository();
                rr.DeleteUserRatings((Guid)mu.ProviderUserKey);
                rr.Save();

                //Delete user mail stat
                StatViewsRepository svr = new StatViewsRepository();
                svr.DeleteUserViews((Guid)mu.ProviderUserKey);
                svr.Save();

                //Delete user section
                UsersRepository ur = new UsersRepository();
                ur.DeleteUserSections((Guid)mu.ProviderUserKey);
                ur.Save();

                ur.DeleteUser(username);                

                return true;
            }

            return false;
        }

        public override string GeneratePassword()
        {
            string newPassword = string.Empty;
            var rnd = new Random();
            int length = (this.MinRequiredPasswordLength < 14) ? 14 : this.MinRequiredPasswordLength;

            for (int i = 0; i < length; i++)
            {
                newPassword += characters[rnd.Next(characters.Length)];
            }

            return newPassword;
        }
    }
}
